<template>
  <div style="overflow: auto"  v-wechat-title="'留言板'">
    <div class="ghost-container">
      <div class="ghost">
        <div class="ghostEyes"></div>
        <div class="ghostDimples"></div>
        <div class="ghostFeet">
          <div class="ghostFoot"></div>
          <div class="ghostFoot"></div>
          <div class="ghostFoot"></div>
          <div class="ghostFoot"></div>
        </div>
      </div>
      <div class="shadow"></div>
    </div>
    <!--  <img src="https://assets.codepen.io/285131/winking-girl.png"/>  -->
    <!--    <img src="https://assets.codepen.io/285131/hat-man.png"/>-->
    <el-card class="message-box-card">
      <el-form :model="msgForm" ref="msgForm" style="margin: 10px 0 100px 0">
        <el-form-item prop="content" :rules="[{ required: true, message: '内容不能为空',trigger:'change'}]">
          <el-input class="gray-bg-input"
                    v-model="msgForm.content"
                    type="textarea"
                    :rows="3"
                    autofocus
                    maxlength="100"
                    show-word-limit
                    placeholder="在此输入你想说的话,可以是问题或者建议～无需登录">
          </el-input>
        </el-form-item>
        <el-form-item style="float: right">
          <div class="btn-control">
            <el-button class="btn" type="primary" @click="handleSubmit('msgForm')">留言</el-button>
          </div>
        </el-form-item>
      </el-form>
      <el-divider><span style="font-size: 20px; color: rgba(149,131,131,0.96) ">留言区</span></el-divider>
      <div class="message-container">
        <ul class="message" v-for="(item,index) in msgList">
          <li class="clearfix">
            <img src="https://assets.codepen.io/285131/hat-man.png" class="avatar" alt="" v-if="index & 1 == 1">
            <img src="https://assets.codepen.io/285131/winking-girl.png" class="avatar" alt="" v-else>
            <div class="post-message">
              <div class="meta">{{ DateUtil.toShowTimeFormat(item.createTime) }}
                <span style="color: #82b440;">
                  {{ isIP(item.userId) ? `来自${item.name}小伙伴` : item.name }}
                </span>
                <span
                    @click="handleDelete(item.id)"
                    style="float: right;color: #F56C6C;cursor: pointer"
                    v-if="item.userId == $store.getters.userId || item.userId == msgKey">
                  删除
                </span>
              </div>
              <p>
                {{ item.content }}
              </p>
            </div>
          </li>
        </ul>
      </div>
      <el-pagination
          v-if="total > 0"
          style="text-align: center"
          @current-change="handleQuery"
          :current-page.sync="currentPage"
          :page-size="10"
          layout="prev, pager, next"
          :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import {queryMessage, addMessage, deleteMessage} from '@/api/message'
import {getMsgKey} from "@/utils/auth";
import CommentBox from "@/components/Comment/CommentBox";
import CommentList from "@/components/Comment/CommentList";
import {deleteComment} from "@/api/comment";

export default {
  name: "message",
  components: {CommentBox, CommentList},
  data() {
    return {
      msgForm: {},
      msgList: [],
      currentPage: 1,
      total: 0,
      msgKey: ''
    }
  },
  methods: {
    isIP(ip) {
      let regExp = new RegExp('(?=(\\b|\\D))(((\\d{1,2})|(1\\d{1,2})|(2[0-4]\\d)|(25[0-5]))\\.){3}((\\d{1,2})|(1\\d{1,2})|(2[0-4]\\d)|(25[0-5]))(?=(\\b|\\D))');
      return regExp.test(ip);
    },
    handleSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const userId = this.$store.getters.userId;
          const nickname = this.$store.getters.nickname;
          if (userId && nickname) {
            this.msgForm.userId = userId;
            this.msgForm.name = nickname;
          }
          addMessage(this.msgForm).then(res => {
            if (res.status) {
              this.msgList.unshift(res.data.obj)
              ++this.total
              const ip = res.data.obj.userId;
              this.msgKey = ip
              this.$store.dispatch('setMsgKey', ip)
              this.msgForm={}
            }
          });
        }
      });
    },
    handleQuery() {
      queryMessage(this.currentPage).then(res => {
        this.msgList = res.data.records
        this.total = res.data.total
      })
    },
    handleDelete(id) {
      this.$confirm('是否删除该留言?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteMessage(id).then( res => {
          this.handleQuery()
        })
      }).catch(() => {
      });
    }
  },
  created() {
    this.handleQuery()
    if (!this.$store.getters.userId) {
      const msgKey = getMsgKey();
      if (msgKey) {
        this.msgKey = msgKey;
      }
    }
  }
}
</script>

<style scoped lang="scss">


.message-box-card {
  max-width: 900px;
  //max-height: 700px;
  margin: 15px auto 0 auto;
  position: relative;
  background: #fff;
}

.message {
  margin-top: 20px;
  margin-bottom: 1rem;
  padding-left: 1rem;
  //border-left: .25rem solid #89ddf0;
  font-weight: 700;
  //font-size: 1.2rem;
  //line-height: 1.5rem
}

.message-container::before,
.message-container::after,
.message-container-form::before,
.message-container-form::after {
  content: "";
  display: table;
  clear: both;
}

.message-container {
  padding-left: 15px;
  padding-right: 15px;
}

.message-container ul {
  list-style-type: none;
  padding: 0;
}

.message-container img {
  opacity: 1;
  filter: Alpha(opacity=100);
  -webkit-border-radius: 4px;
  -moz-border-radius: 4px;
  -o-border-radius: 4px;
  border-radius: 4px;
}

.message-container img.avatar {
  position: relative;
  float: left;
  margin-left: 0;
  margin-top: 0;
  width: 65px;
  height: 65px;
  border: 1px solid
}

.message-container .post-message {
  border: 1px solid #eee;
  margin-bottom: 20px;
  margin-left: 85px;
  margin-right: 0px;
  padding: 10px 10px 3px 20px;
  position: relative;
  -webkit-border-radius: 4px;
  -moz-border-radius: 4px;
  -o-border-radius: 4px;
  border-radius: 4px;
  background: #fff;
  color: #6b6e80;
  position: relative;
}

.message-container .meta {
  font-size: 13px;
  color: #aaaaaa;
  padding-bottom: 8px;
  margin-bottom: 10px !important;
  border-bottom: 1px solid #eee;
}

.message-container > ul.message > li > ul {
  list-style-type: none;
  padding: 0;
  margin-left: 85px;
}

.message-container-form {
  padding-left: 15%;
  padding-right: 15%;
  padding-top: 40px;
}

.message-container h3,
.message-container-form h3 {
  margin-bottom: 40px;
  font-size: 26px;
  line-height: 30px;
  font-weight: 800;
}

</style>
